<?php
namespace model;

class CommentModel {
    public $applicationModel ;

    /**
    * __constructor
    */
    public function __construct()     {
       $this->applicationModel = new \model\ApplicationModel();
    }


    /**
    * @param string
    * @return array
    *
    * här returneras en kommentar till en viss blogpost
    */
    public function returnSpecificComment($id)     {
        $this->applicationModel->dbModel->connect();
        $result = mysql_query("SELECT * FROM comments WHERE id='".$id."'");
        $this->applicationModel->dbModel->close();

        $commentText ="";
        $commentDate ="";
        $commentUser ="";
        $commentBlogPostId ="";

        while($row = mysql_fetch_array($result)) {
            $commentUser =$row[1];
            $commentText =$row[2];
            $commentDate =$row[3];
            $commentBlogPostId =$row[4];
        }

        return array ($id, $commentText, $commentDate, $commentBlogPostId, $commentUser);
    }


    /**
    * @param string $blogId
    * @return object
    *
    * här returneras antalet kommentarer till en viss blogpost
    */
    public function returnNumberOfComments($blogId)     {
        $this->applicationModel->dbModel->connect();
        $result = mysql_query("SELECT * FROM comments WHERE blogpostId='".$blogId."'");
        $this->applicationModel->dbModel->close();
        $numberOfComments = mysql_num_rows($result);

        return $numberOfComments;
    }


    /**
    * @param string $id
    * @return array
    *
    * här hämtas alla kommentarer till en viss blogpost ut från databasen
    */
    public function returnSpecificBlogComments($id)     {
        $this->applicationModel->dbModel->connect();
        $result = mysql_query("SELECT * FROM comments WHERE blogpostId='".$id."'");
        $this->applicationModel->dbModel->close();

        while($row = mysql_fetch_array($result)) {
            $results[] = $row;
        }

        if (isset($results)) {
            return $results;
        } else {
           return array ();
        }
    }


    /**
    * @param string $commentUser
    * @param string $commentText
    * @param string $commentDate
    * @param string $id
    *
    * här lägger vi till en ny kommentar till en viss blogpost
    */
    public function addCommentToDatabase($commentUser , $commentText , $commentDate , $id)     {
        $this->applicationModel->dbModel->connect();
        mysql_query("INSERT INTO comments (user, text, date, blogpostId) VALUES ('"
        . $commentUser ."', '". $commentText. "' , '". $commentDate ."', '" .$id. "' )");
        $this->applicationModel->dbModel->close();

        $_SESSION[$this->applicationModel->m_sessionRegisteredCommentOK] = true;
    }


    /**
    * @return boolean
    *
    * här kollar vi om registrering av en ny kommentar lyckats
    */
    public function hasRegisteredComment() {
        if (isset($_SESSION[$this->applicationModel->m_sessionRegisteredCommentOK]) ) {
            if($_SESSION[$this->applicationModel->m_sessionRegisteredCommentOK] == true) {
                $_SESSION[$this->applicationModel->m_sessionRegisteredCommentOK] = false;
                return true;
            } else {
                    return false;
            }
        } else {
                return false;
        }
    }


    /**
    * @param string $updatedCommentText
    * @param string $id
    *
    * här uppdateras kommentaren i databasen
    */
    public function updateCommentInDatabase($updatedCommentText, $id)     {
        $this->applicationModel->dbModel->connect();
        mysql_query("UPDATE comments SET text='". $updatedCommentText ."' WHERE id ='$id'");
        $this->applicationModel->dbModel->close();

        $_SESSION[$this->applicationModel->m_sessionUpdatedCommentOK] = true;
    }


    /**
    * @return boolean
    *
    * här kollar vi om användaren uppdaterat sin kommentar
    */
    public function hasUpdatedComment() {
        if (isset($_SESSION[$this->applicationModel->m_sessionUpdatedCommentOK]) ) {

            if($_SESSION[$this->applicationModel->m_sessionUpdatedCommentOK] == true) {
                $_SESSION[$this->applicationModel->m_sessionUpdatedCommentOK] = false;
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }


    /**
    * @param string $id
    *
    * här tas en specifik kommentar bort från databasen
    */
    public function removeCommentFromDatabase($id)     {
        $this->applicationModel->dbModel->connect();
        mysql_query("DELETE FROM comments WHERE id='".$id."'");
        $this->applicationModel->dbModel->close();
    }


    /**
    * @param string $text
    * @return boolean
    *
    * här kollas om kommentaren är tillräckligt lång
    */
    public function isCommentToShort($text)     {
       if (strlen($text) < $this->applicationModel->m_minNumberCommentText) {
           return true;
       }
       else {
        return false;
       }
    }


    /**
    * @param string $text
    * @return boolean
    *
    * här kollas om kommentaren är för lång
    */
    public function isCommentToLong($text)     {
       if (strlen($text) > $this->applicationModel->m_maxNumberCommentText) {
           return true;
       }
       else {
        return false;
       }
    }


    /**
    * @param string $text
    * @return boolean
    *
    * här kollas om kommentaren innehåller felaktiga tecken tex <>
    */
    public function hasCommentInvalidChars($text)     {
       if (preg_match("/<[^<]+>/",$text,$m) == 1) {                         //<-preg_match() inbyggd metod som kollar regEx
           return true;
       }
       else {
        return false;
       }
    }


}

